<?php
/**
 * Prado Portal.
 *
 * @author Steen Rabol <steen.rabol@gmail.com>
 * @link http://www.pradoportal.dk/
 * @copyright Copyright &copy; 2006,2007,2008 Steen Rabol
 * @license http://www.pradoportal.dk
 * @version $Id: LanguagePortlet.php 289 2008-11-14 05:34:40Z steen.rabol@gmail.com $
 * @package Pradoportal.Porttlets.LanguagePortlet.Edit
 *
 */

Prado::using('Application.Portlets.Portlet');
Prado::using('Application.Common.Data.PortalLanguageRecord');

class LanguagePortlet extends Portlet
{
	public function onLoad($param)
	{
		$langCtrl = null;
		parent::onLoad($param);

		$criteria=new TActiveRecordCriteria();
		$criteria->OrdersBy['displayorder']='asc';
		$criteria->Condition = "active =1";
		$languages = PortalLanguageRecord::finder()->findAll($criteria);

		if($languages)
		{
			$langCtrl	= new PPLanguageControl();
			foreach($languages as $language)
			{
				$ib						= new TImageButton;
				$ib->Text				= $language->name;
				$ib->ID					= 'ib_' . $language->culture;
				$ib->ImageUrl			= "/images/lang/" . $language->country . ".png";
				$ib->Oncommand[]		= array($this,'itemCommand');
				$ib->CommandParameter	= $language->culture;
				$langCtrl->Controls[]	= $ib;
			}
		}

		if($langCtrl !== null)
		{
			$this->LanguagePlaceholder->addParsedObject($langCtrl);
		}
	}

	public function itemCommand($s, $p)
	{
		$this->Application->Culture	= $p->CommandParameter;
		// Set a cookie to remember the language
		$cookie = new THttpCookie($this->Application->Parameters['CookiePrefix'] . "_lang",$p->CommandParameter);
		$cookie->Expire	= time() + 60*60*24*365; // 1 year
		$this->Response->Cookies[] = $cookie;
		$this->Response->reload();
	}
}
?>